import { DynamicComment } from '@/entity/dynamic-comment.entity';
import { DynamicReply } from '@/entity/dynamic-reply.entity';
import { Dynamic } from '@/entity/dynamic.entity';
import { User } from '@/entity/user.entity';
import { REPLY_TYPE } from '@/enum/dynamic';
import { HTTP_CUSTOM_CODE } from '@/enum/http-custom-code';
import { HttpException, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { CommentDto } from './dto/comment.dto';
import { ReplyDto } from './dto/reply.dto';

@Injectable()
export class CommentService {
  constructor(
    @InjectRepository(DynamicComment)
    private readonly dynamicCommentRepository: Repository<DynamicComment>,
    @InjectRepository(DynamicReply)
    private readonly dynamicReplyRepository: Repository<DynamicReply>,
    @InjectRepository(User)
    private readonly userRepository: Repository<User>,
    @InjectRepository(Dynamic)
    private readonly dynamicRepository: Repository<Dynamic>,
  ) {}

  async createComment(commentDto: CommentDto) {
    // 查询动态是否存在
    const dynamicResult = await this.dynamicRepository.findOne({
      where: {
        id: commentDto.commentDynamicId,
      },
    });
    if (!dynamicResult) {
      throw new HttpException(
        '动态不存在！',
        HTTP_CUSTOM_CODE.DYNAMIC_NOT_FOUND,
      );
    }
    const dynamicComment = new DynamicComment();
    Object.assign(dynamicComment, commentDto);
    const result = await this.dynamicCommentRepository.save(dynamicComment);
    result.userInfo = await this.userRepository.findOne({
      where: {
        id: result.commentUserId,
      },
    });
    result.reply = [];
    return result;
  }

  async createReply(replyDto: ReplyDto) {
    const dynamicReply = new DynamicReply();
    Object.assign(dynamicReply, replyDto);
    const result = await this.dynamicReplyRepository.save(dynamicReply);
    result.userInfo = await this.userRepository.findOne({
      where: {
        id: result.replyUserId,
      },
    });
    result.replyTargetUserInfo = await this.userRepository.findOne({
      where: {
        id: result.replyTargetUserId,
      },
    });
    return result;
  }

  async getDynamicComment(dynamicId: number) {
    const qb = this.dynamicCommentRepository.createQueryBuilder('comment');
    const dynamicCommentResult = await qb
      .where('comment_dynamic_id = :dynamicId', {
        dynamicId,
      })
      .leftJoinAndMapOne(
        'comment.userInfo',
        User,
        'user',
        'user.id = comment.comment_user_id',
      )
      .getMany();
    for (let i = 0; i < dynamicCommentResult.length; i++) {
      dynamicCommentResult[i].reply = await this.dynamicReplyRepository
        .createQueryBuilder('reply')
        .where('comment_id = :commentId', {
          commentId: dynamicCommentResult[i].id,
        })
        .leftJoinAndMapOne(
          'reply.userInfo',
          User,
          'replyUser',
          'replyUser.id = reply.reply_user_id',
        )
        .leftJoinAndMapOne(
          'reply.replyTargetUserInfo',
          User,
          'replyTargetUser',
          'replyTargetUser.id = reply.reply_target_user_id',
        )
        .getMany();
    }

    return dynamicCommentResult;
  }
}
